<template>
  <div class="item-detail-page">
    <!-- 加载状态 -->
    <div v-if="loading" class="loading">
      <div class="loading-spinner"></div>
      <p>加载中...</p>
    </div>

    <!-- 商品详情 -->
    <div v-else-if="itemDetail" class="detail-container">
      <!-- 返回按钮 -->
      <button @click="goBack" class="back-btn">← 返回列表</button>

      <div class="detail-content">
        <!-- 左侧：图片区域 -->
        <div class="image-section">
          <img 
            :src="Array.isArray(itemDetail.images) ? (itemDetail.images[0] || defaultImage) : (itemDetail.images || defaultImage)" 
            :alt="itemDetail.title" 
            class="main-image"
            @error="handleImageError"
          />
        </div>

        <!-- 右侧：商品信息 -->
        <div class="info-section">
          <h1 class="item-title">{{ itemDetail.title }}</h1>
          
          <div class="price-section">
            <span class="price">￥{{ itemDetail.price.toFixed(2) }}</span>
            <span class="view-count">👁️ {{ itemDetail.viewCount }} 次浏览</span>
          </div>

          <div class="meta-info">
            <div class="meta-item">
              <span class="meta-label">成色：</span>
              <span class="meta-value condition">{{ itemDetail.conditionLevel || '未标注' }}</span>
            </div>
            <div class="meta-item">
              <span class="meta-label">分类：</span>
              <span class="meta-value">{{ itemDetail.category || '未分类' }}</span>
            </div>
          </div>

          <div class="description-section">
            <h3 class="section-title">📝 商品描述</h3>
            <p class="description">{{ itemDetail.description || '暂无描述' }}</p>
          </div>

          <div class="action-buttons">
            <button @click="handleContact" class="contact-btn">💬 联系卖家</button>
            <button @click="handleFavorite" class="favorite-btn">❤️ 收藏</button>
          </div>
        </div>
      </div>
    </div>

    <!-- 错误状态 -->
    <div v-else class="error-state">
      <div class="error-icon">😕</div>
      <p>{{ errorMessage || '商品不存在或已下架' }}</p>
      <button @click="goBack" class="back-btn">返回列表</button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { getItemDetail } from '../../api/secondHandItem';
import type { ItemDetailResponse } from '../../api/secondHandItem';

const route = useRoute();
const router = useRouter();

// 使用 base64 编码的 SVG 占位符，避免依赖外部服务
const defaultImage = '';

const loading = ref(false);
const itemDetail = ref<ItemDetailResponse['data'] | null>(null);
const errorMessage = ref('');

// 加载商品详情
const loadItemDetail = async () => {
  const itemId = route.params.id;
  
  if (!itemId) {
    errorMessage.value = '商品ID无效';
    return;
  }
  
  loading.value = true;
  errorMessage.value = '';
  
  try {
    const res = await getItemDetail(Number(itemId));
    
    if (res.success && res.data) {
      itemDetail.value = res.data;
    } else {
      errorMessage.value = res.message || '获取商品详情失败';
    }
  } catch (error: any) {
    console.error('加载商品详情失败:', error);
    errorMessage.value = error.message || '请求失败，请稍后重试';
  } finally {
    loading.value = false;
  }
};

// 返回列表
const goBack = () => {
  router.push('/shop/items');
};

// 联系卖家
const handleContact = () => {
  alert('联系卖家功能开发中...');
};

// 收藏
const handleFavorite = () => {
  alert('收藏功能开发中...');
};

// 图片加载错误处理
const handleImageError = (event: Event) => {
  const target = event.target as HTMLImageElement;
  target.src = defaultImage;
};

// 组件挂载时加载数据
onMounted(() => {
  loadItemDetail();
});
</script>

<style scoped>
.item-detail-page {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

/* 加载状态 */
.loading {
  text-align: center;
  padding: 100px 20px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  max-width: 1200px;
  margin: 0 auto;
}

.loading-spinner {
  width: 50px;
  height: 50px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #52C41A;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 20px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading p {
  color: #999;
  font-size: 16px;
}

/* 详情容器 */
.detail-container {
  max-width: 1200px;
  margin: 0 auto;
}

.back-btn {
  padding: 10px 20px;
  background-color: #f5f5f5;
  color: #666;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s;
  margin-bottom: 20px;
}

.back-btn:hover {
  background-color: #e8e8e8;
  transform: translateX(-4px);
}

.detail-content {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 40px;
  background: white;
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

/* 图片区域 */
.image-section {
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
  border-radius: 12px;
  overflow: hidden;
  min-height: 400px;
}

.main-image {
  width: 100%;
  height: auto;
  max-height: 600px;
  object-fit: contain;
  border-radius: 12px;
}

/* 信息区域 */
.info-section {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.item-title {
  font-size: 28px;
  font-weight: bold;
  color: #333;
  margin: 0;
  line-height: 1.4;
}

.price-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background: linear-gradient(135deg, #fff5f5 0%, #ffe8e8 100%);
  border-radius: 12px;
  border-left: 4px solid #ff4d4f;
}

.price {
  font-size: 36px;
  font-weight: bold;
  color: #ff4d4f;
}

.view-count {
  font-size: 14px;
  color: #999;
}

.meta-info {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 20px;
  background-color: #f9f9f9;
  border-radius: 12px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.meta-label {
  font-weight: 600;
  color: #666;
  font-size: 15px;
  min-width: 60px;
}

.meta-value {
  color: #333;
  font-size: 15px;
}

.meta-value.condition {
  padding: 4px 12px;
  background-color: #e6f7ff;
  color: #1890ff;
  border-radius: 12px;
  font-weight: 500;
  font-size: 13px;
}

.description-section {
  padding: 20px;
  background-color: #fafafa;
  border-radius: 12px;
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0 0 16px 0;
}

.description {
  color: #666;
  line-height: 1.8;
  font-size: 15px;
  white-space: pre-wrap;
  margin: 0;
}

/* 操作按钮 */
.action-buttons {
  display: flex;
  gap: 15px;
  margin-top: auto;
}

.contact-btn,
.favorite-btn {
  flex: 1;
  padding: 14px;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
}

.contact-btn {
  background: linear-gradient(135deg, #52C41A 0%, #389e0d 100%);
  color: white;
}

.contact-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(82, 196, 26, 0.3);
}

.favorite-btn {
  background-color: #fff;
  color: #ff4d4f;
  border: 2px solid #ff4d4f;
}

.favorite-btn:hover {
  background-color: #ff4d4f;
  color: white;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(255, 77, 79, 0.3);
}

/* 错误状态 */
.error-state {
  text-align: center;
  padding: 100px 20px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  max-width: 600px;
  margin: 0 auto;
}

.error-icon {
  font-size: 80px;
  margin-bottom: 20px;
}

.error-state p {
  font-size: 18px;
  color: #666;
  margin: 20px 0 30px 0;
}

/* 响应式 */
@media (max-width: 992px) {
  .detail-content {
    grid-template-columns: 1fr;
    gap: 30px;
    padding: 30px;
  }
  
  .item-title {
    font-size: 24px;
  }
  
  .price {
    font-size: 30px;
  }
}

@media (max-width: 768px) {
  .item-detail-page {
    padding: 10px;
  }
  
  .detail-content {
    padding: 20px;
  }
  
  .item-title {
    font-size: 20px;
  }
  
  .price {
    font-size: 26px;
  }
  
  .action-buttons {
    flex-direction: column;
  }
  
  .image-section {
    min-height: 250px;
  }
}
</style>
